﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AioNightly.Model;
using AioNightly.Dao;
using AioNightly.Common;

namespace AioNightly
{
    public partial class EditNew : System.Web.UI.Page
    {
        private NewNote m_newNote;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUser();
            RefreshNewNote();
        }

        private User m_user;
        private void CheckUser()
        {
            m_user = (User)Session["User"];

            if (m_user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        private void GetItem()
        {
            string id = Request.QueryString["new"];
            if (id != null)
            {
                NewnoteDAO m_newNoteDAO = new NewnoteDAO();
                m_newNote = m_newNoteDAO.GetItemByID(Int32.Parse(id));
            }
        }

        private void RefreshNewNote()
        {
            GetItem();
            if (m_newNote != null)
            {
                lb_newnote.Text = m_newNote.Content;
            }
        }

        private void SwitchVisible()
        {
            bt_edititem.Visible = !bt_edititem.Visible;
            lb_newnote.Visible = !lb_newnote.Visible;

            tb_edititem.Visible = !tb_edititem.Visible;
            bt_submitedit.Visible = !bt_submitedit.Visible;
            bt_canceledit.Visible = !bt_canceledit.Visible;
            bt_delitem.Visible = !bt_delitem.Visible;
        }

        private void RefreshItem()
        {
            GetItem();
            if (m_newNote != null)
            {
                lb_newnote.Text = m_newNote.Content;
            }
        }

        protected void bt_edititem_Click(object sender, EventArgs e)
        {
            SwitchVisible();

            tb_edititem.Text = lb_newnote.Text;
        }

        protected void bt_submitedit_Click(object sender, EventArgs e)
        {
            if (m_newNote != null)
            {
                string m_edititem = tb_edititem.Text.Trim();
                if (m_edititem.Length > 0)
                {
                    NewnoteDAO m_newnoteDAO = new NewnoteDAO();
                    m_newnoteDAO.EditNewitemContent(m_newNote.Id, m_edititem);

                    RefreshItem();

                    lb_newnote.Text = m_edititem;
                    tb_edititem.Text = "";
                    SwitchVisible();
                }
            }
        }

        protected void bt_canceledit_Click(object sender, EventArgs e)
        {
            SwitchVisible();
        }

        protected void bt_delitem_Click(object sender, EventArgs e)
        {
            if (m_newNote != null)
            {
                int productId = m_newNote.ProductId;

                NewnoteDAO m_newnoteDAO = new NewnoteDAO();
                m_newnoteDAO.DeleNewitem(m_newNote.Id);
            }
        }
    }
}